home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-04 | 6.3 KB | 246 lines | [TEXT/MPS ] |
- #
- # File: MessagePassingTest.vu
- #
- # Contains: Demonstration of message passing between multiple targets.
- #
- # Libraries: none
- #
- # Prerequisites: Key Caps needs to be installed on the targets.
- #
- # Written by: Rick Violet
- #
- # Copyright: © 1990-1992 by Apple Computer, Inc., all rights reserved.
- #
- # Change History:
- #
- # 6/19/92 DGG Cleaned up, made parametric, added KeyCaps interface
- # ????? Rick Created
- #
- # To Do:
- #
-
- (************************************************************************************
- * Task GetMyActorName()
- * Get the current actor's name
- ************************************************************************************)
- task GetMyActorName()
- Begin
- match[Actor t:?myName];
- return myName;
- end;
-
- (************************************************************************************
- * Task GetAllOtherActorsNames()
- * Get all of the other actors names, taking out the occurrance of the current
- * actor's name.
- ************************************************************************************)
- task GetAllOtherActorsNames()
- Begin
- match[Actor t:?myName];
- match[Actor t:$otherNames ];
- For i := 1 to Card otherNames
- if otherNames[i] = myName
- begin
- otherNames := remove( i, otherNames );
- end;
- return otherNames;
- end;
-
- (************************************************************************************
- * Task OpenChannel()
- * This task will attempt to open a session with a given actor for communication.
- ************************************************************************************)
- task OpenChannel( receiver := undefined, tries := 10 )
- begin
- if receiver = undefined
- begin
- println "Task OpenChannel - parameter 1 not declared";
- return false;
- end;
- else if (typeOf( receiver ) <> 'string')
- begin
- println "Task OpenChannel - parameter 1 is not of type string";
- return false;
- end;
- else
- begin
- ActorDesc := match[ Actor t:receiver ]!;
- if not ActorDesc
- begin
- println "Task OpenChannel - Failed to match Actor: {receiver}";
- return false;
- end;
- end;
-
- while (( tries > 0 ) and not ( status = "open" ))
- begin
- tries := tries - 1;
- status := openSession( ActorDesc );
- end;
-
- if status = "open"
- begin
- return true;
- end;
- else
- begin
- println "Failed to open channel with Actor : {receiver}";
- return false;
- end;
- end;
-
- (************************************************************************************
- * Task CloseChannel()
- * This task will try to close a session with the specified receiver.
- ************************************************************************************)
- task CloseChannel( receiver, tries := 1 )
- begin
- if receiver = undefined
- begin
- println "Task CloseChannel - parameter 1 not declared";
- return false;
- end;
- else if (typeOf( receiver ) <> 'string')
- begin
- println "Task CloseChannel - parameter 1 is not of type string";
- return false;
- end;
- else
- begin
- ActorDesc := match[ Actor t:receiver ]!;
- if not ActorDesc
- begin
- println "Task CloseChannel - Failed to match Actor: {receiver}";
- return false;
- end;
- end;
-
- while ( tries > 0 ) and not ( status = "done" )
- begin
- tries := tries - 1;
- status := closeSession( ActorDesc );
- end;
-
- if status = "done"
- return true;
- else
- begin
- println "Failed to close channel with Actor : {receiver}";
- return false;
- end;
- end;
-
- (************************************************************************************
- * Task SortList()
- * Simple bubble sort to make sure that all of the actors have the same list!
- ************************************************************************************)
- task SortList( theList )
- begin
- noChanges := false;
- while not noChanges
- begin
- noChanges := true;
- for i := 1 to (card theList - 1)
- if theList[i] > theList[i+1]
- begin
- tempMember := theList[i];
- theList := replace( theList[i+1], i, theList );
- theList := replace( tempMember, i+1, theList );
- noChanges := false;
- end;
- end;
- return theList;
- end;
-
- (************************************************************************************
- * Main Script Here!
- ************************************************************************************)
- script MessageControl(theLeader := 1, typingSpeed := 35)
- begin
-
- # Let's type pretty fast.
- typeSpeed(typingSpeed);
-
- # These are the variables for the different messages being passed.
- AllDoneFlag := "StopTest";
- messageList := { { 1, 2, 3, "start!" },
- { "You say yes…", "I say no…", "You say stop…", "And I say go!" },
- { "ahem" },
- { "How can I be sure?", "When your intrusion is my illusion" },
- { "How can I be sure", "When all the time you changed my mind" },
- { "I asked for more and more", "How can I be sure" },
- { "ahem" },
- { AllDoneFlag}
- };
- totalMessages := card messageList;
-
- # Get and sort all of the actors playing our game.
- match [Actor t:$allActors];
- allActors := SortList(allActors);
-
- # Choose the leader of the game
- MasterDesc := match [actor t:allActors[theLeader]]!;
-
- myName := GetMyActorName();
- println "I am ", myName;
-
- otherNames := GetAllOtherActorsNames();
-
- # Bring up KeyCaps to make things more visually interesting and so you know that
- # info is really being sent.
- select [menuItem t:"Key Caps" m:[menu o:1]];
-
- # Start the game.
- for each player in otherNames
- OpenChannel(player);
-
- # The main part of the game, the master gives orders and the slaves obey!
- if (myName = allActors[theLeader])
- begin
- type k:{returnKey, "I am in control!"};
- msgCount := 1;
- for each singleMessage in messageList
- begin
- for each player in otherNames
- begin
- SlaveDesc := match [actor t:player];
- type k:{returnKey, "Out # {msgCount} of {totalMessages}."};
- wait(1);
- print SlaveDesc.t, " msg {msgCount} of {totalMessages}...";
- if send( SlaveDesc, singleMessage )
- println "Message sent Ok.";
- else
- println "Message send failed.";
- end;
- msgCount := msgCount + 1;
- end;
- end;
- else
- begin
- type k:{returnKey, "I am just a slave!"};
- MoreToGo := true;
- while MoreToGo
- begin
- wait(3);
- msg := receive( MasterDesc );
- println "message = .", msg, ".";
- type k:{returnKey, "In: ", msg};
- if msg = AllDoneFlag
- MoreToGo := false;
- end;
- end;
-
- # Clean up after the game.
- for each player in otherNames
- begin
- CloseChannel(player);
- end;
-
- # Get rid of KeyCaps.
- close [window o:1];
-
- println "All Done";
-
- ### Finished!
-
- end; # MessageControl